setTimeout(function(){ 
	//$("#appName").focus();
	$("#name").focus();
	$("#name").rules("add",{
		remote: {
			url: 'qz/apps/checkName',
			type: "get",
			data: {"id":getQueryString("id")},
			dataType: "JSON", 
			dataFilter:function(data,type){						 
				return data=="false"?false:true;
			}
		}, 		
		messages: {remote: "该应用名称已存在"}
	});
} , parseInt(Math.random() * 1000));

setTimeout(function(){
	$("#code").rules("add",{
		remote: {
			url: 'qz/apps/checkCode',
			type: "get",
			data: {"id":getQueryString("id")},
			dataType: "JSON", 
			dataFilter:function(data,type){		 
				return data=="false"?false:true;
			}
		}, 		
		messages: {remote: "该应用编码已存在"}
	}); 
} , parseInt(Math.random() * 1000));

function selectedDrawing(id,mes,isValid){
	isValid=isValid==undefined?true:false;
	setTimeout(function(){
		$("#"+id).select2({
			 placeholder: mes,
			 allowClear: true
		});
		if(isValid){
			$("#"+id).change(function(){  
				$(this).valid();
			}); 
		}
	} , parseInt(Math.random() * 500));
}

var app = angular.module('qzApp', []);
var appId = getQueryString("id");


app.controller('qzAppController', function($scope, $http,AppService) { 
	$scope.qzApp;
	$scope.searchFlag;
	//load app info
	$scope.view=function(){		
		$http.get(timestamp('qz/channel/getAll/')).success(function(response){
			$scope.channelList=response.data;
			selectedDrawing("channelId","请选择机构");
			selectedDrawing("searchCid","请选择机构");
	    });
			
		$http.get(timestamp('qz/business/getAll/')).success(function(response){
			$scope.businessList=response.data;
			selectedDrawing("businessId","请选择业务分类");
			selectedDrawing("searchBid","请选择业务分类");
	    });
		
		$http.get(timestamp('sys/dic/getByGroup/yylx')).success(function(response){
			$scope.typeList=response.data;
			selectedDrawing("type","请选择应用类型，必选项",true);
		 
	    });	
		
		$http.get(timestamp("qz/apps/get/"+appId)).success(function (response) {
			$scope.qzApp = response.data;
		});
	}
	
	
	//refresh
	$scope.refreshData=function(){
		$scope.view();
	}
	//reset
	$scope.appReset=function(){
		$scope.view();
	}
	//back
	$scope.appCancle=function(){
		redirectUrl("manage/apps/","list.jsp");
	}
	//get associated interfaces
	$scope.queryAssocatedInters=function(){
		$scope.interList;		 
	
		AppService.listAssoInter(appId).success(function (response) {		 
			$scope.interList = response.data;
		});	    
	}
	
	//get interfaces matched the type of app
	$scope.queryMatched=function(){	
		$scope.toggleSearchFalse();
		$scope.searchFlag=1;
		var param={};
		$scope.matchedInterList;
 
		for(var k in $scope.qzApp){			
			param[k] = $scope.qzApp[k];
		} 
		
		param['name']='';
		AppService.list(param).success(function (response) {
			$scope.matchedInterList = response.data;
		});	    
	}
	
	//search interface by given param
	$scope.searchInter=function(){	
		$scope.searchFlag=2;
		var param={"appId":appId,"name":$("#searchname").val(),"channelId":$("#searchCid").val(),"businessId":$("#searchBid").val()};
		$scope.matchedInterList={};
		 
		AppService.list(param).success(function (response) {
			$scope.matchedInterList = response.data;
		});	    
	}
	
	$scope.view();
	$scope.queryAssocatedInters();
	
	
	//save 
	$scope.appSave=function(){
	   if($("#myForm").valid()){
		   $http.put('qz/apps/modify/', $scope.qzApp).success(function(){
			   redirectUrl("manage/apps/","list.jsp");
           });
	   }
	}
	//associate app to interface
	$scope.associate=function(interId,appId){	
		
		var param={"channelInterId":interId,"appId":appId}; 		 
		$http.put('qz/apps/associateInter/', param).success(function(){			
			$scope.queryAssocatedInters(appId);
			if($scope.searchFlag==1){
				$scope.queryMatched();
			}else{
				$scope.searchInter();
			}
			
		});	   
	}
	//remove association prompt
	$scope.rmAssoTip=function(id,mes){
    	$scope.delItem={id:id,mes:mes};
    	$('#box-del').modal({
    		show: true
    	});    	
    }
	//remove association
	$scope.rmAsso=function(id){
		$http.put('qz/apps/assoDel/'+id).success(function(response){		
			$('#box-del').modal('hide');		 
			$scope.queryAssocatedInters(appId);
			$scope.queryMatched();
	    });
	}
	
	//set search state visible or not
	$scope.searchState = {
		show: false
	}
	$scope.toggleSearch = function() {
		$scope.searchState.show = !$scope.searchState.show;
	}
	$scope.toggleSearchFalse = function() {
		$scope.searchState.show = false;
	}
});

app.factory('AppService', function ($http) {
	
	var list = function (param) {		
		return $http.put('qz/apps/getMatchInters/',param);
	};
	var listAssoInter = function (id) {
		return $http.get(timestamp('qz/apps/getInters/'+id));
	}
	return {
		list:function(param){
			return list(param);
		},
		listAssoInter:function(id){
			return listAssoInter(id);
		}
	}
});